﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsForm.cs" company="Rly?">
//   WUT?
// </copyright>
// <summary>
//   Manages default application settings
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Eeeek
{
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;

    using Eeeek.Properties;

    /// <summary>
    /// Manages default application settings
    /// </summary>
    public partial class SettingsForm : Form
    {
        /// <summary>
        /// The configuration file settings
        /// </summary>
        private readonly ConfigurationFileSettings configurationFileSettings = new ConfigurationFileSettings(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsForm"/> class.
        /// </summary>
        public SettingsForm()
        {
            this.InitializeComponent();
            this.LoadCurrentSettings();
            this.PopulateApplicationsList();

            if (this.LogFile == null)
            {
                this.AlertMissingLogFile();
            }
        }

        /// <summary>
        /// Gets the default delay between clicks in milliseconds.
        /// </summary>
        internal int DefaultDelayBetweenClicksInMilliseconds
        {
            get
            {
                return (int)this.DefaultDelayBetweenClicks.Value;
            }
        }

        /// <summary>
        /// Gets the default delay between iterations.
        /// </summary>
        internal int DefaultDelayBetweenIterations
        {
            get
            {
                return (int)this.DelayBetweenIterations.Value;
            }
        }

        /// <summary>
        /// Gets the default number of repetitions.
        /// </summary>
        internal int DefaultNumberOfRepetitions
        {
            get
            {
                return (int)this.NumberOfRepetitions.Value;
            }
        }

        /// <summary>
        /// Gets the target application to use.
        /// </summary>
        internal string TargetApplicationToUse
        {
            get
            {
                return this.TargetApplication.SelectedText;
            }
        }

        /// <summary>
        /// Gets the size of session menu.
        /// </summary>
        internal int SizeOfSessionMenu
        {
            get
            {
                return (int)this.NumberOfSessions.Value;
            }
        }

        /// <summary>
        /// Gets the click down up delay.
        /// </summary>
        internal int ClickDownUpDelay
        {
            get
            {
                return (int)this.ClickLength.Value;
            }
        }

        /// <summary>
        /// Gets the batch start delay in seconds.
        /// </summary>
        internal int BatchStartDelayInSeconds
        {
            get
            {
                return (int)this.StartDelay.Value;
            }
        }

        /// <summary>
        /// Gets the log file.
        /// </summary>
        internal FileInfo LogFile
        {
            get
            {
                return string.IsNullOrWhiteSpace(this.LogFileLabel.Text) ? null : new FileInfo(this.LogFileLabel.Text);
            }
        }

        /// <summary>
        /// Alerts the missing log file.
        /// </summary>
        private void AlertMissingLogFile()
        {
            this.LogFileGroupBox.ForeColor = Color.Red;
        }

        /// <summary>
        /// Populates the applications list.
        /// </summary>
        private void PopulateApplicationsList()
        {
            foreach (var process in Process.GetProcesses().Where(process => !string.IsNullOrWhiteSpace(process.MainWindowTitle)))
            {
                this.TargetApplication.Items.Add(process.MainWindowTitle);
            }

            if (!this.TargetApplication.Items.Contains(this.configurationFileSettings.TargetWindowTitle))
            {
                this.TargetApplication.Items.Add(this.configurationFileSettings.TargetWindowTitle);
            }

            this.TargetApplication.SelectedItem = this.configurationFileSettings.TargetWindowTitle;
        }

        /// <summary>
        /// Loads the current settings.
        /// </summary>
        private void LoadCurrentSettings()
        {
            this.DefaultDelayBetweenClicks.Value = this.configurationFileSettings.DefaultDelayBetweenClicksInMilliseconds;
            this.DelayBetweenIterations.Value = this.configurationFileSettings.DefaultDelayBetweenIterationsInSeconds;
            this.NumberOfRepetitions.Value = this.configurationFileSettings.DefaultNumberOfRepetitions;
            this.NumberOfSessions.Value = this.configurationFileSettings.NumberOfPreviousSessionsInTheMenu;
            this.ClickLength.Value = this.configurationFileSettings.MouseButtonUpDownDelayInMilliseconds;
            this.StartDelay.Value = this.configurationFileSettings.PrebatchStartDelayInSeconds;
            this.LogFileLabel.Text = (this.configurationFileSettings.LogFile != null) ? this.configurationFileSettings.LogFile.FullName : string.Empty;
        }

        /// <summary>
        /// Handles the click event of the LogFileBrowseButton
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void LogFileBrowseButtonClick(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog { Title = Resources.SettingsForm_LogFileBrowseButtonClick_Browse_for_Log_File })
            {
               if (openFileDialog.ShowDialog() == DialogResult.OK)
               {
                   this.LogFileLabel.Text = openFileDialog.FileName;
                   this.configurationFileSettings.LogFile = new FileInfo(this.LogFileLabel.Text);
                   this.LogFileGroupBox.ForeColor = Color.Black;
               }
            }
        }
    }
}
